package com.nginxmanager.controller;

import com.nginxmanager.domain.Conf;
import com.nginxmanager.domain.Model;
import com.nginxmanager.server.config.nginxConf.ModelFileConf;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-01-11.
 */
@RestController
@RequestMapping("/nginx")
@Api(tags = "Nginx模块配置项Api")
public class ModelConf {
    private Conf conf = new Conf();
    private ModelFileConf modelFileConf = new ModelFileConf();

    @GetMapping("/findModelConf")
    @ApiOperation("查看指定配置模块")
    public ResponseEntity<List<String>> find(@RequestParam("按照规范填写url例如：D:\\nginx")String address, @RequestParam("key")String key ){
        List<String> list = new ArrayList<String>();
        conf.setKey(key);
        try {
            list= modelFileConf.get(address,conf);
        }catch (Exception e){
            return ResponseEntity.status(412).body(null);
        }
        return new ResponseEntity<List<String>>(list, HttpStatus.valueOf(200));
    }

    @PostMapping("/addModelConf")
    @ApiOperation("添加模块项")
    public ResponseEntity<String> add(@RequestParam("按照规范填写url例如：D:\\nginx")String address,@RequestBody Model conf1){
        List<String> list = new ArrayList<String>();
        list = modelFileConf.get(address,conf);
        if (!list.isEmpty()){
            return ResponseEntity.status(412).body("配置项已经存在");
        }
        try {
            modelFileConf.add(address,conf1);
        }catch (Exception e){
            return ResponseEntity.status(412).body("I do not know what happen");
        }
        return  new ResponseEntity<String>(conf1.toString(), HttpStatus.valueOf(200));
    }

    @DeleteMapping("/delModelConf")
    @ApiOperation("删除模块项")
    public ResponseEntity<String> deleda(@RequestParam("按照规范填写url例如：D:\\nginx")String address,@RequestBody Model conf1){
        try {
            modelFileConf.delete(address,conf1);
        }catch (Exception e){
            return ResponseEntity.status(412).body(null);
        }
        return  new ResponseEntity<String>(conf1.toString(), HttpStatus.valueOf(200));
    }
}
